const mysql = require('mysql')

const config = {
    host: "localhost", //ip地址
    port: '3306', //端口号
    user: 'root', //数据库用户
    password: "1356056894", //数据库密码
    database: "oceanweb" //数据库名
}

exports.db = (sql, sqlParams) => {

    sqlParams = sqlParams || []
        // sqlParams = sqlParams == null ? [] : sqlParams
    return new Promise((resolve, resject) => {
        const pool = mysql.createPool(config)
        pool.getConnection((err, conn) => {
            if (!err) {

                conn.query(sql, sqlParams, (e, results) => {
                    if (!e) {
                        console.log(results);
                        resolve(results)
                        conn.destroy()
                    } else {
                        console.log("sql:", e)
                        resject(e)
                    }
                })
            } else {
                console.log("conn err:", err);
                resject(err)
            }
        })
    })
}